This notebook merges CORINE and LISA land cover datasets and converts CORINE land cover types into LISA land cover types based on a specified conversion map.
This notebook uses GRASS GIS (7.0.4), and must be run inside of a GRASS environment (start the jupyter notebook server from the GRASS command line). GRASS GIS
landcover_corine — name of CORINE land cover vector map
landcover_lisa — name of LISA land cover vector map
landcover — name of the merged land cover dataset
conversion_map_filename — path to csv file mapping land cover types between CORINE and LISA datasets
In [1]:
landcover_corine = 'landcover_corine'
In [2]:
corine_type_field = 'value'
In [3]:
landcover_lisa = 'landcover_lisa'
In [4]:
lisa_type_field = 'lbtyp'
In [5]:
landcover = 'landcover'
In [75]:
conversion_map_filename = ""
In [67]:
import pandas
import numpy as np
from grass import script as gscript
from grass.pygrass.vector import VectorTopo
from grass.pygrass.vector.table import DBlinks
connect to attribute table
In [8]:
def connectToAttributeTable(map):
vector = VectorTopo(map)
vector.open(mode='r')
dblinks = DBlinks(vector.c_mapinfo)
link = dblinks[0]
return link.table()
overlay a vector map on an underlying vector map using a specified operator ('and', 'or', 'not', etc.)
In [9]:
def overlay(overlay, underlay, operator, output):
gscript.read_command('v.overlay',
ainput=overlay,
binput=underlay,
operator=operator,
output=output,
overwrite=True)
export vector map to a shapefile
In [73]:
def exportVectorToShapefile(map, output):
gscript.read_command('v.out.ogr',
input=map,
format='ESRI_Shapefile',
output=output,
flags='e',
overwrite=True)
# flags='e' — use ESRI-style .prj file format
cut out the extent of LISA land cover dataset from the CORINE dataset
In [22]:
overlay(overlay=landcover_corine, underlay=landcover_lisa, operator='not', output=landcover + '_extended')
combine the cut CORINE dataset with the LISA dataset
In [27]:
overlay(overlay=landcover_lisa, underlay=(landcover + '_extended'), operator='or', output=landcover)
get attribute table
In [14]:
table = connectToAttributeTable(map=landcover)
table.filters.select()
columns = table.columns
cursor = table.execute()
result = np.array(cursor.fetchall())
cursor.close()
combined_table = pandas.DataFrame(result, columns=columns).set_index('cat')
load convsersion map
In [12]:
conversion_map = pandas.read_csv(conversion_map_filename).set_index('CORINE')
In [13]:
conversion_map
Out[13]:
In [15]:
combined_lisa_type_field = 'a_' + lisa_type_field
combined_corine_type_field = 'b_a_' + corine_type_field
remapped = []
for index, row in combined_table[[combined_lisa_type_field, combined_corine_type_field]].iterrows():
if row[combined_lisa_type_field] is not None:
value = row[combined_lisa_type_field]
else:
value = conversion_map.get_value(row[combined_corine_type_field], 'LISA')
remapped.append(value)
In [16]:
remapped_table = pandas.DataFrame({'cat':combined_table.index,
'type':remapped}).set_index('cat')
replace attribute table
In [68]:
table.create([(u'cat', u'integer'), (u'type', u'integer')], overwrite=True)
In [69]:
table.insert([ (index, row['type']) for index, row in remapped_table.iterrows() ], many=True)
Out[69]:
In [70]:
table.conn.commit()
In [74]:
exportVectorToShapefile(landcover, "")